<template>
  <div>
    <!-- 健康信息 -->
    <!-- 病史介绍 -->
    <div class="header">
      <div class="header_left">
        <span class="line" />
        <span>病史介绍</span>
      </div>
    </div>
    <el-form ref="ruleFormRef" :model="ruleForm" label-width="auto">
      <el-form-item label="现病史" style="width: 800px">
        <el-checkbox-group v-model="ruleForm.health.nowillnessArr">
          <el-checkbox v-for="item in nowillnessList" :value="item.name" name="type">
            {{ item.name }}
          </el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <el-form-item label="既往病史" style="width: 800px">
        <el-checkbox-group v-model="ruleForm.health.oldillnessArr">
          <el-checkbox v-for="item in oldillnessList" :value="item.name" name="type">
            {{ item.name }}
          </el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <el-form-item label="其他疾病">
        <el-input v-model="ruleForm!.health.otherillness" type="textarea" />
      </el-form-item>
      <!-- 老人自理情况 -->
      <div class="header">
        <div class="header_left">
          <span class="line" />
          <span>老人自理情况</span>
        </div>
      </div>
      <!-- 表格 -->
      <el-table :data="ruleForm.selfCares" style="width: 100%">
        <el-table-column prop="id" label="排序" />
        <el-table-column prop="name" label="维度" />
        <el-table-column label="操作">
          <template #default="scope">
            <el-radio-group v-model="scope.row.val" @change="handleradio(scope.row, scope.$index)">
              <el-radio value="正常">正常</el-radio>
              <el-radio value="轻度依赖">轻度依赖</el-radio>
              <el-radio value="重度依赖">重度依赖</el-radio>
            </el-radio-group>
          </template>
        </el-table-column>
      </el-table>
      <el-form-item label="其他特殊情况说明" style="margin-top: 30px">
        <el-input v-model="ruleForm!.health.otherCase" type="textarea" />
      </el-form-item>

      <div class="header">
        <div class="header_left">
          <span class="line" />
          <span>老人自理情况</span>
        </div>
      </div>
      <!-- 体检项目 -->
      <el-table :data="ruleForm.checkupList" style="width: 100%">
        <el-table-column prop="name" label="体检项目" width="180" />
        <el-table-column label="资料">
          <template #default="scope">
            <img v-for="item in scope.row.checkups" :key="item.id" :src="imgUrl + item.picture"
              style="width: 50px;height: 50px;" alt="">
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <el-upload :headers="headers" class="avatar-uploader" :action="BaseUrl + '/api/upload/add'"
              :show-file-list="false"
              :on-success="(response) => handleAvatarSuccess(response, scope.$index, scope.row.name)"
              :before-upload="beforeAvatarUpload">
              <div class="uploadContent">
                <div class="btns">
                  <el-button type="primary">上传图片</el-button>
                </div>
              </div>
            </el-upload>
          </template>
        </el-table-column>
      </el-table>
    </el-form>
  </div>
</template>

<script setup lang="ts">
import { reactive, inject } from "vue";
import { AddParams, checkupsData } from "@/api/market/elderly/type";
import ELTable, { TableData } from "@/components/ELTable.vue";
import { getDiseaseList, getSelfcareList, getCheckupitems } from "@/api/market/elderly/elderly";
import { FormInstance, UploadProps } from "element-plus";
import { CompressImage } from "@/utils/imageUtils";

const ruleFormRef = ref();

//现病史
let nowillness = ref<string[]>([]);
watch(nowillness, () => {
  ruleForm!.health.nowillness = nowillness.value.join(",");
});
const nowillnessList = ref();
//现病史
let oldillness = ref<string[]>([]);
watch(oldillness, () => {
  ruleForm!.health.oldillness = oldillness.value.join(",");
});
const oldillnessList = ref();

//添加参数
const ruleForm: AddParams = inject<AddParams>("ruleForm") as AddParams;

//体检项目表格数据
const checkupList = ref<any[]>([]);



//获取疾病列表
const getDiseaseData = async () => {
  let res = await getDiseaseList();
  //现病史列表
  nowillnessList.value = res.list;
  //以往病史列表
  oldillnessList.value = res.list;
};

//根路径
const BaseUrl = import.meta.env.VITE_APP_API_URL;
// 图片根路径
const imgUrl = ref(import.meta.env.VITE_IMG_URL);
const headers = { Authorization: localStorage.getItem("token") };

//老人自理情况
const handleradio = (data: any, index: number) => {
  ruleForm!.selfCares[index] = { id:data.id,name: data.name, val: data.val };
  if (ruleForm!.selfCares) {
    ruleForm!.selfCares = ruleForm!.selfCares.filter((item: any) => item !== null);
  }
};

const tableData = ref<any[]>([])


// //获取老人自理情况
// const getElderlySelfcare = async () => {
//   let res = await getSelfcareList();
//   tableData.value = res.list.map(item => ({ id: item.id, name: item.name, val: "" }))
// }

//获取体检项目列表
const getCheckupData = async () => {
  let res = await getCheckupitems();
  checkupList.value = res.list;
}

// watch([() => ruleForm.checkups, () => checkupList], (res) => {
//   if (res[0].length && res[1].value.length) {

//     console.log("图片监听");

//     checkupList.value = checkupList.value.map(item => ({ ...item, checkups: res[0].filter(sItem => item.name == sItem.name) }));
//   }
// })

// watch([() => ruleForm.selfCares, () => tableData], (res) => {
//   if (res[0].length && res[1].value.length) {
//     // console.log("6788",res);
//     tableData.value.map((item: any) => res[0].filter(sItem => item.name == sItem.name ? item.val = sItem.val : ""))
//   }
// })




//文件上传前回调
const beforeAvatarUpload: UploadProps["beforeUpload"] = (rawFile) => {
  let url = URL.createObjectURL(rawFile);
  return CompressImage(url, 50);
  // return true
};
//文件上传
const handleAvatarSuccess = (response: any, index: number, data: string) => {
  if (response.code == 10000) {

    console.log("添加图片");

    ruleForm.checkups.push({ name: data, picture: response.data.url })
    ruleForm.checkupList[index].checkups?.push({ name: data, picture: response.data.url })
    // checkupList.value.map(item => {
    //   item.checkups = ruleForm.checkups.filter(sItem => item.name == sItem.name);
    // })
  }
};

onMounted(async () => {
  getDiseaseData();
  // getElderlySelfcare();
  getCheckupData();
});
</script>

<style scoped>
.header {
  display: flex;
  align-items: center;
  height: 50px;

  .header_left {
    display: flex;
    align-items: center;
    margin-right: 30px;

    .line {
      display: block;
      width: 6px;
      height: 20px;
      margin-right: 8px;
      background-color: #529bfd;
    }
  }
}
</style>
